<template>
  <div class="home">
    <el-row>
      <el-col :span="24">
        <el-button type="primary" @click="dialogVisible = true">添加盆栽</el-button>
      </el-col>
    </el-row>
    <el-divider></el-divider>
    <el-table
      :data="penzaiList"
      border
      style="width: 100%">
      <el-table-column
        prop="name"
        label="名称"
        width="180">
      </el-table-column>
      <el-table-column
        label="状态"
        width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">非正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button @click="handleUpdate(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="currentPenzai.id ? '更新' : '添加'"
      :visible.sync="dialogVisible"
      width="20%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false">
      <el-form label-width="60px">
        <el-form-item label="名称">
          <el-input v-model="currentPenzai.name"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="currentPenzai.state"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onDialogCancel">取 消</el-button>
        <el-button type="primary" @click="onDialogOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Home',

  data () {
    return {
      penzaiList: [
        { id: 1, name: '文竹', state: 0 },
        { id: 2, name: '吊兰', state: 1 },
        { id: 3, name: '仙人掌', state: 1 },
        { id: 4, name: '含羞草', state: 0 },
        { id: 5, name: '绿萝', state: 1 }
      ],
      currentPenzai: {},
      dialogVisible: false
    }
  },

  created () {
    this.getPenzaiList()
  },

  methods: {
    // 获取盆栽列表数据
    getPenzaiList () {
      this.$ajax.get('/findAll').then(res => {
        // console.log(res)
        // 注意：根据真实情况，将res或其内部的数据对下一行代码打开注释并赋值
        this.penzaiList = res
      })
    },

    // 根据id更新盆栽
    handleUpdate (data) {
      this.currentPenzai = data
      this.dialogVisible = true
    },

    // 弹窗点击确认按钮
    onDialogOk () {
      const { currentPenzai } = this
      if (currentPenzai.id) {
        // 更新盆栽
        this.$ajax.put('/updatePenzai', currentPenzai).then(res => {
          console.log(res)
          // 获取更新后的盆栽列表，【备注：理想状态应该是直接修改现有数据，等待下次进入页面后再重新获取新数据，减少不必要的数据请求】
          this.getPenzaiList()
          this.currentPenzai = {} // 清空一下数据
          this.dialogVisible = false
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '更新失败!'
          })
        })
      } else {
        // 新增盆栽
        this.$ajax.post('/addPenzai', currentPenzai).then(res => {
          console.log(res)
          // 获取新增后的盆栽列表，【备注：理想状态应该是直接修改现有数据，等待下次进入页面后再重新获取新数据，减少不必要的数据请求】
          this.getPenzaiList()
          this.currentPenzai = {} // 清空一下数据
          this.dialogVisible = false
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '新增失败!'
          })
        })
      }
    },

    // 弹窗点击取消按钮
    onDialogCancel () {
      this.currentPenzai = {}
      this.dialogVisible = false
    },

    // 根据id 删除盆栽
    handleDelete (data) {
      this.$confirm(`此操作将永久删除盆栽: ${data.name}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$ajax.delete('/delPenzaiById', { id: data.id }).then(res => {
          console.log(res)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
